package com.choosefine.it.company.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import javax.validation.constraints.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.CompanyInfo;
import com.choosefine.it.basedata.service.CompanyInfoService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.Assert;
import com.choosefine.it.company.model.dto.CompanyUserInfoDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 建筑公司-个人信息
 * @author chenjian
 * @date 2016-12-7 下午4:41:12
 */
@RequestMapping(value="/companyUserInfo")
@RestController
@Api(value="建筑公司个人信息",description="陈健")
public class CompanyUserInfoController extends BaseController {
	@Autowired
	CustomRestTemplate restTemplate;
	
	@Autowired
	CompanyInfoService companyInfoService;
	
	@Autowired
	ObjectMapper jsonObjectMapper;


	Logger logger=LoggerFactory.getLogger(CompanyUserInfoController.class);
	
	@RequestMapping(value="/getUserInfo/{userCode}",method=RequestMethod.GET)
	@ApiOperation(value="获取建筑公司个人信息",response=CompanyUserInfoDTO.class)
	@ApiImplicitParam(name = "userCode", value = "租户编号", required = false, dataType = "String", paramType = "path")
	public String getUserInfo(@PathVariable String userCode) throws JsonProcessingException{
		CompanyInfo companyInfo = companyInfoService.findByUserCode(userCode);
		
		CompanyUserInfoDTO companyUserInfoDTO = new CompanyUserInfoDTO();
		Assert.assertNotNull(userCode, ResultCode.FAILED,ResultCode.MSG_PARAMS_NULL);
		BeanUtils.copyProperties(companyInfo, companyUserInfoDTO);
		companyUserInfoDTO.setHeadIconUrl(companyInfo.getCompanyPicurl());
		companyUserInfoDTO.setIdcardNum(companyInfo.getContactIdcard());
		companyUserInfoDTO.setPhoneNumber(companyInfo.getContactPhoneNumber());
		companyUserInfoDTO.setRealName(companyInfo.getContactName());
		companyUserInfoDTO.setEnterpriseTypeName(companyInfoService.getUnitType(companyInfo.getEnterpriseType()));
		companyUserInfoDTO.setCompanyTypeName(companyInfoService.getCompanyType(companyInfo.getCompanyType()));
	    return responseSuccess(companyUserInfoDTO, ResultCode.SUCCESS,ResultCode.MSG_SUCCESS);
	}
	
	@RequestMapping(value="/getUserHeadImage/{userCode}",method=RequestMethod.GET)
	@ApiOperation("获取建筑公司个人信息-头像")
	public String getUserHeadImage(@PathVariable String userCode) throws JsonProcessingException{
		String headImage = companyInfoService.findHeadImageByUserCode(userCode,false);
		if(StringUtils.isNotEmpty(headImage)){
			
		    return responseSuccess(headImage, ResultCode.SUCCESS,ResultCode.MSG_SUCCESS);
		}else{
			return responseFail(ResultCode.FAILED,ResultCode.MSG_PARAMS_ERROR);
		}
	}
	
	@RequestMapping(value="/getUserLargeHeadImage/{userCode}",method=RequestMethod.GET)
	@ApiOperation("获取建筑公司个人信息-大头像")
	public String getUserLargeHeadImage(@PathVariable String userCode) throws JsonProcessingException{
		String headImage = companyInfoService.findHeadImageByUserCode(userCode,true);
		if(StringUtils.isNotEmpty(headImage)){
			
		    return responseSuccess(headImage, ResultCode.SUCCESS,ResultCode.MSG_SUCCESS);
		}else{
			return responseFail(ResultCode.FAILED,ResultCode.MSG_PARAMS_ERROR);
		}
	}
}
